package com.winexpress.entity;

import com.winexpress.helper.AESEncryptor;
import com.winexpress.helper.Config;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

@Entity
public class OrderHistory implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToOne
    private Customer customer;
    @OneToMany
    private List<OrderLine> orderLines;
    private double orderDiscount;
    private String shippingAddress;
    private String creditCardNumber;
    private boolean isClosed = false;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OrderHistory)) {
            return false;
        }
        OrderHistory other = (OrderHistory) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.winexpress.entity.Purchase[ id=" + id + " ]";
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getShippingAddress() throws Exception {
        if (shippingAddress != null) {
            return AESEncryptor.decrypt(Config.seed, shippingAddress);
        }
        return null;
    }

    public void setShippingAddress(String shippingAddress) throws Exception {
        this.shippingAddress = AESEncryptor.encrypt(Config.seed, shippingAddress);
    }

    public boolean isIsClosed() {
        return isClosed;
    }

    public void setIsClosed(boolean isClosed) {
        this.isClosed = isClosed;
    }

    public String getCreditCardNumber() throws Exception {
        if (creditCardNumber != null) {
            return AESEncryptor.decrypt(Config.seed, creditCardNumber);
        }
        return null;
    }

    public void setCreditCardNumber(String creditCardNumber) throws Exception {
        this.creditCardNumber = AESEncryptor.encrypt(Config.seed, creditCardNumber);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }

    public double getOrderDiscount() {
        return orderDiscount;
    }

    public void setOrderDiscount(double orderDiscount) {
        this.orderDiscount = orderDiscount;
    }
}
